import { objDeepCopy,getDateString } from "@/assets/js/common.js";
let Controller = {
	created() {
		this.init();
		this.initdata();
	},
	activated() {
		this.init();
		this.initdata();
	},
	methods: {
		init() {
			this.Model.Outofstock = this.$route.query.flag;
			this.Model.OutofstockBtn = this.$route.query.status;
		},
		initdata() {
			this.Model.orderId = this.$route.query.id;		//订单id
			this.requestDetail(this,this.Model.orderId);
		},
		//详情接口
		requestDetail(that,id) {
			this.$ajax({
				method: 'get',
				url:this.apiTest+'/supplier/whWarehouse/outStorageOrder/detail/'+id,
				//url:'/api/whWarehouse/outStorageOrder/detail/'+id,
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					console.log(result)
					//应出商品
					that.Model.tableData = objDeepCopy(result.goodsOutList);
					//实出商品
					that.Model.tableDataOutNews = objDeepCopy(result.goodsActualOutList);
					//操作日志
					//that.Model.tableData4 = objDeepCopy(result.whWarehouseLogList);
					//表头信息
					that.Model.tabFormInline = [{
							label: '售后出库单号',
							value: result.outStorageNumber,
						},
						{
							label: '单据状态',
							value: result.status,
						},
					];
					//步骤条
					var stepData =  objDeepCopy(result.whWarehouseStatus);
					var stepArr = [];
					for (let i = 0; i < stepData.length; i++) {
						if(stepData[i].code == result.status){
							that.Model.stepOption.activeStep = i;
						}
						let obj = {
							title:stepData[i].codeName,
							description:stepData[i].createTime,
						}
						stepArr.push(obj);
					}
					that.Model.stepOption.step = objDeepCopy(stepArr);
					//出库单基础信息
					that.Model.tableFormInline = [{
							label: '出库单类型',
							value: result.orderType
						},
						{
							label: '售后订单',
							value: result.saleNumber
						},
						{
							label: '是否开票',
							value: result.isInvoice
						},
						{
							label: '来源渠道',
							value: result.originChannel
						},
						{
							label: '发货仓位',
							value: result.deliverySpace
						},
						{
							label: '是否签名',
							value: result.isSign
						},
						{
							label: '创建人',
							value: result.createBy
						},
						{
							label: '操作人',
							value: result.operator
						},
						{
							label: '打印次数',
							value: result.printNo
						},
						{
							label: '备注',
							value: result.remark
						}
					];
					//个人信息（旧）
					that.Model.tableFormInline2 = [{
							label: '收件人姓名',
							value: result.userInfoLeft.receiveName
						},
						{
							label: '收件人手机号',
							value: result.userInfoLeft.receivePhone
						},
						{
							label: '收货人详细地址',
							value: result.userInfoLeft.receiveAddress
						},
					];
					//个人信息（新）
					if(!Object.keys(result.userInfoRight).length==0){
						that.Model.AdressChange = true;
						that.Model.tableFormInlineNew = [{
								label: '收件人姓名',
								value: result.userInfoRight.receiveName
							},
							{
								label: '收件人手机号',
								value: result.userInfoRight.receivePhone
							},
							{
								label: '收货人详细地址',
								value: result.userInfoRight.receiveAddress
							},
						];
					}
					//实出包材
					if(result.materialStandardList.length){
						that.Model.tableData2Out = objDeepCopy(result.materialStandardList);
					}
					//实出包材详情
					if(result.materialActualOutList.length){
						that.Model.PackmaterNewsStatus = true;
						that.Model.tableData2News = objDeepCopy(result.materialActualOutList);
					}
					//判断有无物流
					if(result.logisticsCompany){
						that.Model.interflow = true;
						that.Model.interflowcompany = result.logisticsCompany;
					}
					if(result.trackingNumber){
						that.Model.interflow = true;
						that.Model.interflownumber = result.trackingNumber;
					}
					//判读是否出现创建物流按钮
					that.Model.logisticsBtn = result.isCreateLogistics;
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
		},
		setStepData(list, status) {
			
		},
		//请求扫码入库 
		skuEntryAjax(that,code){
			var tableData = this.Model.tableData;
			var tableDataOut = this.Model.tableDataOut;
			tableData.forEach(item=>{
				if(code == item.barCode){
					tableDataOut.push(item);
					//包材
					this.Model.PackmaterStatus = true;
				}
			});
		},
		
		//扫码入库
		checkSku(){
			var code = this.Model.writeSku;
			var tableDataOut = this.Model.tableDataOut;
			var codeIndexOf = this.valueInData(code,tableDataOut);
			if(codeIndexOf == -1){
				this.skuEntryAjax(this,code);
			}else{
				this.tips(this,'warning','条码已存在')
			}
		},
		//遍历 判断是否存在数组中
		valueInData(val,data){
			var indexof = -1;
			data.forEach(function(value,index,array){
				Object.keys(value).forEach(function(key){
					// console.log(key,value[key]);
					if(key == 'barCode' && value[key] == val){
						indexof = 0;
						return;
					}
				});
			})
			return indexof;
		},
		//删除 表格数据
		deleteTableData(){
			console.log(2)
			var that = this;
			var TableSelectData = this.Model.TableSelectData;
			var tableDataOut = this.Model.tableDataOut;
			if(TableSelectData.length == 0){
				this.tips(this,'warning','请先勾选需要删除的数据');
				return;
			}
			this.$confirm('此操作将删除相应数据, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				TableSelectData.forEach(function(value,index,array){
					tableDataOut.forEach(function(val,idx,arr){
						if(value == val){
							that.Model.tableDataOut.splice(idx,1);
						}
					})
				})
				this.$message({
					type: 'success',
					message: '删除成功!'
				});
			}).catch(() => {
			           
			});
		},
		//确认出库
		ToOutofstock() {
			var arr = [];
			var tableDataOut = this.Model.tableDataOut;
			for (let i = 0; i < tableDataOut.length; i++) {
				arr.push(tableDataOut[i].serialNumber);
			}
			if(tableDataOut.length == 0){
				this.tips(this,'warning','请选择数据');
				return;
			}
			//判断是否有获取物流单号
			if(this.Model.logisticsBtn){
				this.tips(this,'warning','请创建物流单号');
				return;
			}
			//提交的数据 formInline
			var formInline = this.Model.formInline;
			formInline.goodIdList = arr;
			formInline.packMaterialList = this.Model.tableData2Out;
			formInline.whWareHouId = [this.Model.orderId];
			this.confirmNewAjax(this,formInline);
		},
		//确认出库 请求
		confirmNewAjax(that,params){
			var that = this;
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whWarehouse/outStorageOrder/confirm',
				//url:'/api/whWarehouse/outStorageOrder/confirm',
				data:params
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					if(result == 1){
						that.tips(that,'success','操作成功');
						that.requestDetail(this,this.Model.orderId);
						that.$router.push({
							path:'afterSaleLeaveDetail',
							query: {
								id:this.Model.orderId,
								flag:"1",
								status:"1"
							}
						});
						that.Model.Outofstock = that.$route.query.flag;
					}
				}else{
					that.tips(that,'warning',data.msg)
				}
		
			});
		},
		//取消
		goBack() {
			this.$router.back(-1);
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		materialSelectionChange(val) {
			console.log(val);
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		//包材确定
		surePackmaterials() {
			var mainTableData = this.Model.materialsTableData;
			var outAmountarr = [];//数量大于1的数组
			mainTableData.forEach(item=>{
				if(item.outAmount >= 1){
					outAmountarr.push(item);
				}
			});
			this.tips(this,'success','添加成功！');
			this.Model.tableData2Out=outAmountarr;
			this.Model.Packmaterials = false;
		},
		//出库
		Changeoutofstock() {
			this.Model.Outofstock = '0';
			this.Model.OutofstockBtn = '1';
		},
		//获取物流单号弹窗
		getlogistics() {
			this.Model.logisticsDilog = true;
		},
		//根据物流类型获取物流单号
		logisticsNum() {
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whWarehouse/outStorageOrder/createSF',
				//url:'/api/whWarehouse/outStorageOrder/createSF',
				data:{
					deliveryType:this.Model.logisticsSelect,
					whHouseIds:[this.Model.orderId]
				}
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					this.Model.logisticsNumbers = data.result;	
				}else{
					this.tips(this,'warning',data.msg)
				}
			});
		},
		//确认物流
		Surelogistics() {
			var that = this;
			var params = [];
			var obj = {
				whHouseId:this.Model.orderId,
				deliveryType:this.Model.logisticsSelect,
				deliveryNo:this.Model.logisticsNumbers,
				expressPayment:this.Model.expressWay,
			};
			params.push(obj);
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whWarehouse/outStorageOrder/saveLogistics',
				//url:'/api/whWarehouse/outStorageOrder/saveLogistics',
				data:params
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					setTimeout(function(){
						that.tips(that,'success','保存成功！');
						that.Model.logisticsDilog = false;
						that.requestDetail(that,that.Model.orderId);
					},1000);
					
				}else{
					that.tips(that,'warning',data.msg)
				}
			});
		},
		//包材编辑
		PackmaterialsAdd() {
			this.Model.Packmaterials = true;
			var that = this;
			this.$ajax({
				method: 'get',
				url:this.apiTest+'/sale/saleCategoryPack/materialList',
				//url:'/api/sale/saleCategoryPack/materialList',
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					this.Model.materialsTableData = objDeepCopy(result);
					var tableDataOut = this.Model.tableData2Out;
					var tableDataAll = that.Model.materialsTableData;
					tableDataOut.forEach(function(value,index,array){
						tableDataAll.forEach(function(val,idx,arr){
							if(value.id == val.id){
								val.outAmount = value.outAmount;
							}
						})
					})
				}else{
					that.tips(that,'warning',data.msg)
				}
			});
		},
		onTabChange(val) {
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		//展开收缩
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === true) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = false;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = true;
			}
		},
		tips(that,type,message) {
			that.$message({
				message: message,
				type: type
			});
		},
	},
	filters:{
		companyFilters:function(value){
			var val;
			value == '10' ? val = '顺丰' : val = 'EMS';
			return val;
		},
	}
}
export default Controller;
